from rest_framework import serializers

from goods.models import SPU, GoodsCategory, Brand


class SPUSerializer(serializers.ModelSerializer):
    # 关联对象嵌套序列化返回
    brand = serializers.StringRelatedField(read_only=True)
    brand_id = serializers.IntegerField()

    category1 = serializers.PrimaryKeyRelatedField(read_only=True)
    category1_id = serializers.IntegerField()

    category2 = serializers.PrimaryKeyRelatedField(read_only=True)
    category2_id = serializers.IntegerField()

    category3 = serializers.PrimaryKeyRelatedField(read_only=True)
    category3_id = serializers.IntegerField()

    # category1 = serializers.StringRelatedField(read_only=True)
    # category1_id = serializers.IntegerField()
    #
    # category2 = serializers.StringRelatedField(read_only=True)
    # category2_id = serializers.IntegerField()
    #
    # category3 = serializers.StringRelatedField(read_only=True)
    # category3_id = serializers.IntegerField()

    class Meta:
        model = SPU
        fields = '__all__'


class BrandSerializer(serializers.ModelSerializer):
    """
        品牌信息序列化器
    """

    class Meta:
        model = Brand
        fields = ('id', 'name')


# class CategorySerializer3(serializers.ModelSerializer):
#     class Meta:
#         model = GoodsCategory
#         fields = "__all__"
#
#
# class CategorySerializer2(serializers.ModelSerializer):
#     category2 = CategorySerializer3(many=True)
#
#     class Meta:
#         model = GoodsCategory
#         fields = "__all__"
#
#
# class CategorySerializer(serializers.ModelSerializer):
#     category = CategorySerializer2(many=True)
#
#     class Meta:
#         model = GoodsCategory
#         fields = "__all__"


class CategorySerializer(serializers.ModelSerializer):
    # 三级分类序列化器
    class Meta:
        model = GoodsCategory
        fields = '__all__'
